package uit.cinemainfo;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class TheaterListViewAdapter extends ArrayAdapter<Theater>{
	private Context context;
	private int layoutResourceId;
	private Theater[] data= null;
	
	public TheaterListViewAdapter(Context context, int layoutResourceId, Theater[] data){
		super(context, layoutResourceId, data);
		this.context = context;
		this.layoutResourceId = layoutResourceId;
		this.data = data;
	}
	
	static class TheaterHolder{
		public TextView NameTheater;
		public TextView AddTheater;
		public TextView TelTheater;
		public ImageView ImgTheater;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent){
		View row = convertView;
		TheaterHolder holder = new TheaterHolder();
		try{
			if(row == null){
				LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				row = inflater.inflate(layoutResourceId, parent, false);
			}
			else{
				holder = (TheaterHolder)row.getTag();
			}
			
			Theater pro = data[position];
			if(pro != null){
				holder.NameTheater = (TextView)row.findViewById(R.id.NameTheater);
				holder.AddTheater = (TextView)row.findViewById(R.id.AddTheater);
				holder.TelTheater = (TextView)row.findViewById(R.id.TelTheater);
				holder.ImgTheater = (ImageView)row.findViewById(R.id.ImgTheater);
				row.setTag(holder);
				
				holder.NameTheater.setText(pro.namecine);
				holder.AddTheater.setText(context.getString(R.string.add)+": "+pro.addcine);
				holder.TelTheater.setText(context.getString(R.string.tel)+": "+pro.telcine);
				if(pro.imgtheater.endsWith("hungvuong")){holder.ImgTheater.setImageResource(R.drawable.hungvuong);}
				if(pro.imgtheater.endsWith("parkson")){holder.ImgTheater.setImageResource(R.drawable.parkson);}
				if(pro.imgtheater.endsWith("nguyendu")){holder.ImgTheater.setImageResource(R.drawable.nguyendu);}
				if(pro.imgtheater.endsWith("nguyentrai")){holder.ImgTheater.setImageResource(R.drawable.nguyentrai);}
				this.notifyDataSetChanged();
			}
		} catch (Exception e){Log.d("My check in adapter", e.getMessage());}
		return row;
	}
}
